﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.Web.ASPxGridView;

public partial class Category : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["current"] = "Product Category";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }
            
        }
        else
        {
            Response.Redirect("index.aspx");
        }
    }
    
    #region Validation

    protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        try
        {
            // Displays the error row if there is at least one error.
            if (e.Errors.Count > 0) e.RowError = "Please, fill all fields.";

            if (e.NewValues["Cat_ID"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Cat_ID"],
                "Please Enter Category ID");
            }
            //if (e.NewValues["Description"] != null && e.NewValues["Description"].ToString().Length < 2)
            //{
            //    AddError(e.Errors, ASPxGridView1.Columns["Description"],
            //    "Description must be at least two characters long.");
            //}
            if (e.NewValues["Cat_Name"] == null || e.NewValues["Cat_Name"].ToString().Length < 2)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Cat_Name"],
                "Category Name must be at least two characters long.");
            }
            if (e.NewValues["Status"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Status"],
                "Please Select Status");
            }

            if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0)
                e.RowError = "You must enter a value for all required fields";
        }
        catch
        {
            e.RowError = "Please check the data you entered are in correct format";
        }
    }

    void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
    {
        try
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }
        catch
        {
            Response.Redirect("404.aspx");
        }
    }
        
    protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
    {
        try
        {
            // Validates the edited row if it isn't a new row,.
            if (!ASPxGridView1.IsNewRowEditing)
                ASPxGridView1.DoRowValidation();
        }
        catch
        {
            Label2.Text= "Please Check The Data You Entered are in Correct Format";
            Label2.Visible = true;
        }

    }

    #endregion

    //to hide Delete button 
    protected void ASPxGridView1_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
    {
        
        //if (e.ButtonType == DevExpress.Web.ASPxGridView.ColumnCommandButtonType.Delete)
        //{
        //    e.Visible = User.IsInRole("administrators");
        //    e.Enabled = User.IsInRole("administrators");
        //}
    }
   
    }
